var par = new Object();
var grid;
par.bankRequestType = bankRequestType;
$(function () {

    //Date range picker
    $('#sendRange').daterangepicker({
        timePicker: false, autoUpdateInput: false, locale: {
            applyLabel: '确认',
            cancelLabel: '取消',
            fromLabel: '起始时间',
            toLabel: '结束时间',
            customRangeLabel: '自定义',
            firstDay: 1,
            format: 'YYYY-MM-DD'
        },
        ranges: {
            '今日': [moment().startOf('day'), moment()],
            '昨日': [moment().subtract('days', 1).startOf('day'), moment().subtract('days', 1).endOf('day')],
            '最近7日': [moment().subtract('days', 6), moment()],
            '最近30日': [moment().subtract('days', 29), moment()],
            '本月': [moment().startOf("month"), moment().endOf("month")],
            '上个月': [moment().subtract(1, "month").startOf("month"), moment().subtract(1, "month").endOf("month")]
        },
        opens: 'right',    // 日期选择框的弹出位置
        separator: '至',
        showWeekNumbers: true,     // 是否显示第几周
        format: 'YYYY-MM-DD'

    }, function (start, end, label) {
        var show = start.format('YYYY-MM-DD') + ' - ' + end.format('YYYY-MM-DD');
        $('#sendRange').val(show);
    });


    $("#queryBtn").click(function () {
        grid.fnSettings().ajax.data = getQueryObj();
        grid.fnDraw();
    });

    $('#exportBtn').click(function () {
        var durl = queryPath+"/bankOrderExport.do";
        var par = getQueryObj();
        var form=$("<form>");//定义一个form表单
        form.attr("style","display:none");
        form.attr("target","");
        form.attr("method","post");
        form.attr("action",durl);
        for(var key in par)
        {
            var value = par[key];
            if(typeof(value)!="undefined")
            {
                var input1=$("<input>");
                input1.attr("type","hidden");
                input1.attr("name",key);
                input1.attr("value",value);
                form.append(input1);
            }

        }
        $("body").append(form);//将表单放置在web中
        form.submit();//表单提交
    });

    $("#resetBtn").click(function () {
        makeClean();
    });

    grid =  $("#grid").dataTable( {
        paging: true,//分页
        serverSide: true,
        searching:false,
        bLengthChange: false,
        ordering:false,
        responsive: true,
        ajax:{
            url:queryPath+"/bankOrderListReply.do",
            data:par,
            dataSrc: function (data) {
                $.extend(data, {
                    iTotalDisplayRecords: data.total,
                    iTotalRecords: data.total
                });
                return data.rows;
            }
        },
        columnDefs:[
            {
                defaultContent: "",
                targets: "_all"
            }
        ],
        columns: [
            { data: "requestNo" },
            { data: "bankCode" ,render:function (data, type, full, meta) {
               return bankCoe[data];
            } },
            { data: "bankRequestNo"
            },
            { data: "requestType",render:function (data, type, full, meta) {
                if(data =='0'){
                    return "查询";
                }else{
                    return "拒付";
                }
            } },
            { data: "tradeDate",render:function (data, type, full, meta) {
                if (data != ''&&data.length>10) {
                    data = data.substr(0,10);
                }
                return data;
            } },
            { data: "tradeMoney" },
            {data: "interceptState",render:function (data, type, full, meta) {
                if(data =='1')
                {
                    return "是";
                }else{
                    return "否";
                }

            }},
            {data:'emergencyState',render:function(data, type, full, meta){
                if(data =='1')
                {
                    return "紧急";
                }else{
                    return "普通";
                }
            }},
            { data: "bankAttachCount" },
            { data: "jdAttachCount" },
            { data: "bankRequestState",render:function(data, type, full, meta){
                if(data =='0')
                {
                    return "未发送";
                }else if(data =='1'){
                    return "未回复";
                }else{
                    return "已回复";
                }

            } },
            { data: "bankRequestState" },
            { data: "tradeDate" ,render:function (data, type, full, meta) {
                if (data != ''&&data.length>10) {
                    data = data.substr(0,10);
                }
                return data;
            } },
            { data: "realName" ,render:function (data, type, full, meta) {
                return "<a href=\"#\" onclick=\"detail('"+full.id+"','"+full.bankRequestState+"')\">详情</a>"
            }
            }
        ],
        language: {
            lengthMenu: '<select class="form-control input-xsmall">' + '<option value="1">1</option>' + '<option value="10">10</option>' + '<option value="20">20</option>' + '<option value="30">30</option>' + '<option value="40">40</option>' + '<option value="50">50</option>' + '</select>条记录',//左上角的分页大小显示。
            paginate: {//分页的样式内容。
                previous: "上一页",
                next: "下一页",
                first: "第一页",
                last: "最后"
            },

            zeroRecords: "没有内容",//table tbody内容为空时，tbody的内容。
            //下面三者构成了总体的左下角的内容。
            info: "总共_PAGES_ 页，显示第_START_ 到第 _END_ ，筛选之后得到 _TOTAL_ 条，初始_MAX_ 条 ",//左下角的信息显示，大写的词为关键字。
            infoEmpty: "0条记录",//筛选为空时左下角的显示。
            infoFiltered: ""//筛选之后的左下角筛选提示，
        },
        paging: true,
        pagingType: "full_numbers",//分页样式的类型

    });

    if(bankRequestType == '0')
    {
        $(".bankRequestTypeCls").each(function (index,item) {
            $(item).html('银行订单号');
        }) ;
    }else{
        $(".bankRequestTypeCls").each(function (index,item) {
            $(item).html('银行卡号');
        }) ;
    }

    $.each(bankCoe , function (index, item) {
       $('#bankCode').append(" <option value=\'"+index+"\' >"+item+"</option>")
    });
});

function detail(id ,bankRequestState){
    if(bankRequestState == '2')
    {
        window.location.href = queryPath+"/toBankOrderDetailReplyPage.do?requestId="+id;
    }else{
        window.location.href = queryPath+"/toBankOrderDetailReplyModifyPage.do?requestId="+id;
    }
}


function getQueryObj() {
    par = new Object();
    par.bankRequestType = bankRequestType;
    if($('#bankRequestNo').val().length >0){
        par.bankRequestNo = $('#bankRequestNo').val();
    }
    if($('#requestType').val().length >0){
        par.requestType = $('#requestType').val();
    }
    if($('#emergencyState').val().length >0)
    {
        par.emergencyState =$('#emergencyState').val();
    }
    if($('#bankCode').val().len >0){
        par.bankCode =$('#bankCode').val();
    }
    if($('#interceptState').val().length>0){
        par.interceptState = $('#interceptState').val();
    }
    if($('#bankRequestState').val().length>0){
        par.bankRequestState = $('#bankRequestState').val();
    }
    var sendRange = $('#sendRange').val();
    if(sendRange && sendRange.length>0)
    {
        var sendSeg = sendRange.split(" - ");
        if(sendSeg[0] == sendSeg[1])
        {
            par.sendStart = sendSeg[0]+" 00:00:00";
            par.sendEnd = sendSeg[0]+" 59:59:59";
        }else{
            par.sendStart = sendSeg[0]+" 00:00:00";
            par.sendEnd = sendSeg[1]+" 59:59:59";
        }
    }
    return par;
}

function makeClean()
{
    $('input').each(function (index ,item) {
        $(this).val('');
    })
    $('select').each(function (index, item) {
        $(this).val('');
    });
    $('textarea').each(function () {
        $(this).val('');
    })
}